package pl.edu.agh.administracja.networkconfig.model;

/**
 *
 * @author piotrek
 */
public class IpV4Address {
    
    public static final String EMPTY_ADDRESS = "   .   .   .   ";
    public static final String ADDRESS_PATTERN = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
    public static final int ADDRESS_LENGTH = 4;
    
    private short[] bytes = new short[4];
    
    public IpV4Address(){
    }
    
    public static IpV4Address fromString(String str){
        String[] splitted = str.split("\\.");
        if(splitted.length != ADDRESS_LENGTH)
            throw new RuntimeException("Nieprawidlowy adres");
        
        IpV4Address address = new IpV4Address();
        
        for(int i=0; i<ADDRESS_LENGTH; i++){
            address.bytes[i] = Short.parseShort(splitted[i]);
        }
        
        return address;
    }
    
    @Override
    public String toString(){
        StringBuilder builder = new StringBuilder();
        for(int i=0; i<ADDRESS_LENGTH; i++){
            if( i!= 0)
                builder.append(".");
            
            builder.append(bytes[i]);
        }
        
        return builder.toString();
    }
}
